﻿using System;
using System.Collections;
using System.Collections.Generic;
using Game.modules.game.tower;
using GameFramework.data;

using GameFramework.skill.effect;

using UnityEngine;

namespace GameFramework.skill
{
    public class SkillManager:MonoBehaviour
    {
        public List<Skill> _skills = new List<Skill>();
        
        private Skill nowReleaseSkill;//当前正在释放的技能

        private void Start()
        {
            List<int> temp = new List<int>();
            temp.Add(1);
            addSkill(temp);
        }

        public void addSkill(List<int> skillIds)
        {
            //先检测是否有这个技能了
            //创建skill对象，对skill里的静态数据和运行时数据进行初始化
            foreach (var skillId in skillIds)
            {
                Skill skill = createSkill(skillId);
                _skills.Add(skill);
            }
        }

        Skill createSkill(int id)
        {
            Skill skill = new Skill();
            skill.init(id,gameObject);
            return skill;
        }

        public bool canRelease(Skill skill)
        {
            if (skill == null)
            {
                Debug.Log("技能为空");
                return false;
            }

            SkillConfig skillConfig = skill.skillConfig;
            if (DateTime.Now.Second - skill.lastReleasTime < skillConfig.cd)
            {
                Debug.Log("CD中。。。");
                //TODO 抛出CD的事件
                return false;
            }
            
            //根据技能释放着的类型，查找数据

            //判断消耗
            //判断状态
            //判断是否是连击技能，上一个技能是否释放完成
            
            //检测无目标是否释放
            if (skill.targets.Count == 0 && skill.selector!=null)
            {
                skill.selector.selectTarget(skill, skill.owner.transform);
            }
            
            //
            if (skillConfig.targetType == 2)//需要选定目标
            {
                if (skill.targets.Count==0)
                {
                    if (skillConfig.searchType != 0)//需要搜寻视野范围内的敌人，搜到了则寻路过去
                    {
                        Debug.Log("无目标，需要视野范围内自动搜寻，未实现！");
                        //TODO 视野范围内查找目标
                        // PlayerCtl playerCtl = skill.owner.GetComponent<PlayerCtl>();
                        // playerCtl.moveTo(Pos);
                    }
                    Debug.Log("未找到目标。。");
                    return false;
                }
            }

            if (skillConfig.targetType == 3)//需要选定地点
            {
                if (skill.targetPos.Count == 0)
                {
                    Debug.Log("无释放点");
                    return false;
                }
            }

            //
            //TODO 判断其他条件
            return true;
        }

        public Skill getSkill(int skillId)
        {
            for (var i = 0; i < _skills.Count; i++)
            {
                if (_skills[i].skillId == skillId)
                {
                   return _skills[i];
                }
            }

            return null;
        }

        //生成技能
        public void releaseSkill(int skillId)
        {
            Skill skill = getSkill(skillId);

            if (skill == null)
            {
                Debug.LogWarning("未拥有此技能，无法释放："+skillId);
                return;
            }

            
            if (!canRelease(skill))
            {
                Debug.Log("不能释放技能");
                return;
            }
            
            
            
            //TODO 技能指示器
            
            //TODO 发出释放前的事件，可能buff有监听
            
            //TODO 播放动画前摇
            //创建技能预制体
            // GameObject skillgo = GameObjectPool.instance.CreateObject(data.prefabName, data.skillPrefab, transform.position, transform.rotation);
            // GameObject skillgo = GameObject.Instantiate(skillPrefab,transform.position,quaternion.identity);
            //传递技能数据给技能释放器
            // SkillDeployer deployer = skillgo.GetComponent<SkillDeployer>();
            // deployer.Skill = skill;
            //释放器释放技能
            // deployer.DeploySkill();
            
            //正式释放
            skill.lastReleasTime = DateTime.Now.Second;
            foreach (ISkillEffect skillEffect in skill.effects)
            {
                try
                {
                    Debug.Log("执行技能效果！");
                    skillEffect.excute(skill);
                }
                catch (Exception e)
                {
                    Debug.LogError(e);
                }

            }
            //TODO 发出释放后的事件，可能buff有监听
            // StartCoroutine(CoolTimeDown(skill));//开启冷却
        }
        
        //协程实现技能冷却
        private IEnumerator CoolTimeDown(Skill data)
        {
            data.lastReleasTime = DateTime.Now.Second;
            while ((DateTime.Now.Second-data.lastReleasTime)<data.skillConfig.cd)
            {
                yield return new WaitForSeconds(0.5f);
            }
        }
        
    }
}